import OlOverlay from 'ol/Overlay';
function Overlay(map) {
    this.map = map;
    this.overlay = null;
    this.handleClick = null;
    this.handleClickEndfun; //用于点击结束返回要素
}

/**
 * @description: 设置ol弹窗
 * @param {} dom
 * @param {fun} fun 点击结束返回要素
 * @return {*}
 */
Overlay.prototype.setOverlay = function (dom, fun) {
    if (fun) {
        this.handleClickEndfun = fun;
    }
    this.overlay = new OlOverlay({
        element: dom //绑定 Overlay 对象和 DOM 对象的
    });
    this.map.addOverlay(this.overlay);

    // 创建一个箭头函数保留 this 上下文，并保存引用
    this.handleClick = (e) => {
        e.stopPropagation();
        let feature = this.map.forEachFeatureAtPixel(e.pixel, (feature) => {
            return feature;
        });
        if (feature) {
            this.handleClickEndfun(feature);
        }
    };

    this.map.on('click', this.handleClick);

    return this.overlay;
};

/**
 * @description: 移除地图点击事件
 * @return {void}
 */
Overlay.prototype.removeOverlay = function () {
    this.map.un('click', this.handleClick); // 解绑 click 事件
};

export default Overlay;
